package com.coship.vasms.adstorm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.coship.vasms.adstorm.common.log.JLogger;
import com.coship.vasms.adstorm.common.log.LoggerFactory;
import com.coship.vasms.adstorm.dao.AccesslogDao;
import com.coship.vasms.adstorm.model.Accesslog;
import com.coship.vasms.adstorm.service.LogDataService;

public class LogDataServiceImpl implements LogDataService,
		ApplicationContextAware {

	private static JLogger logger = LoggerFactory
			.getLogger(LogDataServiceImpl.class);

	private ApplicationContext applicationContext;

	private AccesslogDao accesslogDao;

	/**
	 * Description
	 * 
	 * @param opType
	 * @param content
	 */
	public void save(Integer opType, String content) {
		try {
			Accesslog log = new Accesslog();
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			int uid = (Integer) session.getAttribute("suserid");
			log.setUserId(uid);
			log.setOpType(opType);
			log.setContent(content);
			log.setCreateTime(new Date());
			accesslogDao.save(log);
		} catch (Exception e) {
			logger.info("发生异常,cause:", e);
		}

	}

	/**
	 * Description 日志分页查询
	 * 
	 * @param startDate
	 * 
	 * @param endDate
	 * 
	 * @param opType
	 * 
	 * @param userId
	 * 
	 * @param content
	 * 
	 * @param pageId
	 * 
	 * 
	 * @param pageSize
	 * @return
	 */
	public List<Accesslog> pagedSearch(Date startDate, Date endDate,
			Integer opType, Integer userId, String content, int pageId,
			int pageSize) {
		HashMap<String, Object> pMap = new HashMap<String, Object>();
		if (startDate != null)
			pMap.put("startTime", startDate);
		if (endDate != null)
			pMap.put("endTime", endDate);
		if (opType != null)
			pMap.put("opType", opType);
		;
		if (userId != null)
			pMap.put("userId", userId);
		if (content != null && !content.equals(""))
			pMap.put("content", content);
		return accesslogDao.queryByCondition(pMap);
	}

	public List<Accesslog> pagedSearch() {
		return accesslogDao.queryAlllog();
	}

	@Override
	public void setApplicationContext(ApplicationContext app)
			throws BeansException {
		applicationContext = app;
	}

	public AccesslogDao getAccesslogDao() {
		return accesslogDao;
	}

	public void setAccesslogDao(AccesslogDao accesslogDao) {
		this.accesslogDao = accesslogDao;
	}

}
